import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class AdminManager {
    private String adminUsername = "admin";
    private String adminPassword = "ynuinfo#777";

    public boolean adminLogin(Scanner scanner) {
        System.out.print("请输入管理员用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入管理员密码: ");
        String password = scanner.nextLine();

        // 这里可以实现实际的管理员验证逻辑
        return adminUsername.equals(username) && adminPassword.equals(password);
    }


    public void adminFunctionality(List<RegularUser> users, List<Product> products, Scanner scanner) {
        boolean inAdminManagement = true;
        while (inAdminManagement) {
            System.out.println("\n管理员管理菜单");
            System.out.println("1. 修改自身密码");
            System.out.println("2. 用户管理");
            System.out.println("3. 商品管理");
            System.out.println("4. 退出登录");
            System.out.print("请选择操作: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    changeAdminPassword(scanner);
                    break;
                case 2:
                    manageUsers(new ArrayList<>(), scanner); // 假设用户列表为空；可替换为实际用户列表
                    break;
                case 3:
                    productManagement(new ArrayList<>(), scanner); // 假设商品列表为空；可替换为实际商品列表
                    break;
                case 4:
                    inAdminManagement = false; // 退出
                    System.out.println("已退出登录。");
                    break;
                default:
                    System.out.println("无效的选择。");
            }
        }
    }

    // 修改管理员密码
    public void changeAdminPassword(Scanner scanner) {
        System.out.print("请输入新密码: ");
        String newPassword = scanner.nextLine();
        this.adminPassword = newPassword; // 更新密码
        System.out.println("密码已修改。");
    }

    // 用户管理
    public void manageUsers(List<RegularUser> users, Scanner scanner) {
        boolean inUserManagement = true;
        while (inUserManagement) {
            System.out.println("\n用户管理菜单");
            System.out.println("1. 列出所有客户信息");
            System.out.println("2. 删除客户信息");
            System.out.println("3. 查询客户信息");
            System.out.println("4. 重置客户密码");
            System.out.println("5. 返回");
            System.out.print("请选择操作: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    listAllCustomers(users);
                    break;
                case 2:
                    deleteCustomer(users, scanner);
                    break;
                case 3:
                    queryCustomer(users, scanner);
                    break;
                case 4:
                    resetUserPassword(users, scanner);
                    break;
                case 5:
                    inUserManagement = false; // 返回
                    break;
                default:
                    System.out.println("无效的选择。");
            }
        }
    }

    // 列出所有客户信息
    public void listAllCustomers(List<RegularUser> users) {
        System.out.println("所有客户信息：");
        if (users.isEmpty()) {
            System.out.println("没有客户记录。");
        } else {
            for (RegularUser user : users) {
                System.out.println("客户ID: " + user.getUsername() + ", 用户名: " + user.getUsername() +
                        ", 级别: " + user.getMembershipLevel() + ", 注册时间: " + user.getRegistrationDate() +
                        ", 消费总金额: " + user.getTotalSpent() + ", 手机号: " + user.getPhoneNumber() +
                        ", 邮箱: " + user.getEmail());
            }
        }
    }

    private void deleteCustomer(List<RegularUser> users, Scanner scanner) {
        System.out.print("请输入要删除的客户ID: ");
        String userId = scanner.nextLine();
        System.out.print("警告: 删除后无法恢复，确定要删除该客户吗？(y/n): ");
        String confirmation = scanner.nextLine();

        if (confirmation.equalsIgnoreCase("y")) {
            boolean removed = users.removeIf(user -> user.getUsername().equals(userId));
            if (removed) {
                System.out.println("客户信息已删除。");
            } else {
                System.out.println("未找到客户信息。");
            }
        } else {
            System.out.println("删除操作已取消。");
        }
    }

    private void queryCustomer(List<RegularUser> users, Scanner scanner) {
        System.out.print("请输入要查询的客户ID或用户名 (输入 'all' 将列出所有客户): ");
        String input = scanner.nextLine();
        if (input.equalsIgnoreCase("all")) {
            listAllCustomers(users);
        } else {
            for (RegularUser user : users) {
                if (user.getUsername().equals(input)) {
                    System.out.println("客户信息: 用户名: " + user.getUsername() + ", 邮箱: " + user.getEmail() +
                            ", 级别: " + user.getMembershipLevel() + ", 注册时间: " + user.getRegistrationDate() +
                            ", 消费总金额: " + user.getTotalSpent() + ", 手机号: " + user.getPhoneNumber());
                    return;
                }
            }
            System.out.println("未找到客户信息。");
        }
    }

    private void resetUserPassword(List<RegularUser> users, Scanner scanner) {
        System.out.print("请输入要重置密码的客户ID: ");
        String userId = scanner.nextLine();
        boolean userFound = false;

        for (RegularUser user : users) {
            if (user.getUsername().equals(userId)) {
                userFound = true;
                System.out.print("请输入新密码: ");
                String newPassword = scanner.nextLine();

                if (isPasswordValid(newPassword)) {
                    user.setPassword(newPassword); // 假设 RegularUser 有 setPassword 方法
                    System.out.println("客户密码已成功重置。");
                } else {
                    System.out.println("新密码不符合要求。");
                }
                break; // 找到用户后退出循环
            }
        }

        if (!userFound) {
            System.out.println("未找到客户ID。");
        }
    }

    private boolean isPasswordValid(String password) {
        return password.length() > 8 && password.matches(".*[A-Z].*") && password.matches(".*[a-z].*")
                && password.matches(".*[0-9].*") ;
    }

    // 商品管理
    public void productManagement(List<Product> products, Scanner scanner) {
        boolean inProductManagement = true;
        while (inProductManagement) {
            System.out.println("\n商品管理菜单");
            System.out.println("1. 列出所有商品的信息");
            System.out.println("2. 添加商品的信息");
            System.out.println("3. 修改商品的信息");
            System.out.println("4. 删除商品的信息");
            System.out.println("5. 查询商品的信息");
            System.out.println("6. 返回");
            System.out.print("请选择操作: ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    listAllProducts(products);
                    break;
                case 2:
                    addProduct(products, scanner);
                    break;
                case 3:
                    modifyProduct(products, scanner);
                    break;
                case 4:
                    deleteProduct(products, scanner);
                    break;
                case 5:
                    queryProduct(products, scanner);
                    break;
                case 6:
                    inProductManagement = false; // 返回
                    break;
                default:
                    System.out.println("无效的选择。");
            }
        }
    }

    private void listAllProducts(List<Product> products) {
        System.out.println("所有商品信息：");
        if (products.isEmpty()) {
            System.out.println("没有商品记录。");
        } else {
            for (Product product : products) {
                System.out.println("商品编号: " + product.getProductId() + ", 商品名称: " + product.getName() +
                        ", 生产厂家: " + product.getManufacturer() + ", 生产日期: " + product.getManufactureDate() +
                        ", 型号: " + product.getModel() + ", 进货价: " + product.getPurchasePrice() +
                        ", 零售价格: " + product.getRetailPrice() + ", 数量: " + product.getQuantity());
            }
        }
    }

    private void addProduct(List<Product> products, Scanner scanner) {
        System.out.print("请输入商品编号: ");
        String productId = scanner.nextLine();
        System.out.print("请输入商品名称: ");
        String name = scanner.nextLine();
        System.out.print("请输入生产厂家: ");
        String manufacturer = scanner.nextLine();
        System.out.print("请输入生产日期 (yyyy-mm-dd): ");
        String manufactureDate = scanner.nextLine(); // 假设这里简化处理，实际应使用日期格式
        System.out.print("请输入型号: ");
        String model = scanner.nextLine();
        System.out.print("请输入进货价格: ");
        double purchasePrice = scanner.nextDouble();
        System.out.print("请输入零售价格: ");
        double retailPrice = scanner.nextDouble();
        System.out.print("请输入库存数量: ");
        int quantity = scanner.nextInt();
        scanner.nextLine(); // 消耗换行符

        Product newProduct = new Product(productId, name, manufacturer, manufactureDate, model, purchasePrice, retailPrice, quantity);
        products.add(newProduct);
        System.out.println("商品已添加。");
    }

    private void modifyProduct(List<Product> products, Scanner scanner) {
        System.out.print("请输入要修改的商品编号: ");
        String productId = scanner.nextLine();
        Product productToModify = findProductById(products, productId);

        if (productToModify != null) {
            System.out.print("请输入新的商品名称 (当前: " + productToModify.getName() + "): ");
            String name = scanner.nextLine();
            System.out.print("请输入新的生产厂家 (当前: " + productToModify.getManufacturer() + "): ");
            String manufacturer = scanner.nextLine();
            System.out.print("请输入新的生产日期 (当前: " + productToModify.getManufactureDate() + "): ");
            String manufactureDate = scanner.nextLine();
            System.out.print("请输入新的型号 (当前: " + productToModify.getModel() + "): ");
            String model = scanner.nextLine();
            System.out.print("请输入新的进货价格 (当前: " + productToModify.getPurchasePrice() + "): ");
            double purchasePrice = scanner.nextDouble();
            System.out.print("请输入新的零售价格 (当前: " + productToModify.getRetailPrice() + "): ");
            double retailPrice = scanner.nextDouble();
            System.out.print("请输入新的库存数量 (当前: " + productToModify.getQuantity() + "): ");
            int quantity = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符
            // 更新商品信息
            productToModify.setName(name);
            productToModify.setManufacturer(manufacturer);
            productToModify.setManufactureDate(manufactureDate);
            productToModify.setModel(model);
            productToModify.setPurchasePrice(purchasePrice);
            productToModify.setRetailPrice(retailPrice);
            productToModify.setQuantity(quantity);
            System.out.println("商品信息已修改。");
        } else {
            System.out.println("未找到商品编号。");
        }
    }

    private void deleteProduct(List<Product> products, Scanner scanner) {
        System.out.print("请输入要删除的商品编号: ");
        String productId = scanner.nextLine();
        Product productToDelete = findProductById(products, productId);

        if (productToDelete != null) {
            System.out.print("警告: 删除后无法恢复，确定要删除该商品吗？(y/n): ");
            String confirmation = scanner.nextLine();

            if (confirmation.equalsIgnoreCase("y")) {
                products.remove(productToDelete);
                System.out.println("商品信息已删除。");
            } else {
                System.out.println("删除操作已取消。");
            }
        } else {
            System.out.println("未找到商品编号。");
        }
    }

    private void queryProduct(List<Product> products, Scanner scanner) {
        System.out.print("请输入商品名称、生产厂家或零售价格进行查询: ");
        String input = scanner.nextLine();
        boolean found = false;

        for (Product product : products) {
            if (product.getName().equalsIgnoreCase(input) ||
                    product.getManufacturer().equalsIgnoreCase(input) ||
                    String.valueOf(product.getRetailPrice()).equals(input)) {
                System.out.println("商品信息: 商品编号: " + product.getProductId() + ", 商品名称: " + product.getName() +
                        ", 生产厂家: " + product.getManufacturer() + ", 生产日期: " + product.getManufactureDate() +
                        ", 型号: " + product.getModel() + ", 进货价: " + product.getPurchasePrice() +
                        ", 零售价格: " + product.getRetailPrice() + ", 数量: " + product.getQuantity());
                found = true;
            }
        }

        if (!found) {
            System.out.println("未找到相关商品信息。");
        }
    }

    private Product findProductById(List<Product> products, String productId) {
        for (Product product : products) {
            if (product.getProductId().equals(productId)) {
                return product;
            }
        }
        return null; // 未找到
    }
}

